#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "log.hpp"
using namespace std;

const string path = "./dict.txt";
const string seg = ":";

extern Log lg;

bool Split(string &line, string &part1, string &part2)
{
    int pos = line.find(seg);
    if (pos != string::npos)
    {
        part1 = line.substr(0, pos);
        part2 = line.substr(pos + 1);
        return true;
    }
    return false;
}

class Translation_
{
public:
    Translation_()
    {
        ifstream in(path);
        if (!in)
        {
            lg("fatal", "ifstream create fail");
            return ;
        }
        string str;
        while (getline(in, str))
        {
            string part1, part2;
            if (Split(str, part1, part2))
            {
                hash_.insert({part1, part2});
            }
        }
    }

    string Translate(const string &key)
    {
        auto ret = hash_.find(key);
        if (ret == hash_.end())
            return "Unkonw";
        return ret->second;
    }
private:
    unordered_map<string, string> hash_;
};

